$(function(){
	update_dom('');
	
	function update_dom(wrapper){
		// reply comment button
        $(wrapper + "[id^=reply]").click(function(e){
        	//get current Id & Pid & language
        	tmp = this.id.split('-');
        	replyId = tmp[1];
       		langId = tmp[2];

         	//set value for form "new-comment-box"
        	$("#pid").attr("value",replyId);
        	$("#title").attr("value",'');
        	$("#new-comment-form [name=language]").val(langId); //langId is parent's language
        	
       		//move box "new-comment-box" arround the list :))
        	$("#new-comment-box").insertAfter("#comment-box-" + replyId).css('width', '80%');
	     	return false; 
		});	//end reply
		
		//new comment
    	$("#new-comment-form").ajaxForm({
			dataType:  'json',
			success: function(json) {
				if(json.success == '1'){
					alert('post success');
					//set default values 
					$("#comment_text").attr("value",'');
					$("#title").attr("value",'');
					$("#pid").attr("value",'0');
					$("a.redraw").trigger("click");
					
					//move box to default place
					$("#new-comment-box").appendTo("#comment-list");
					return false;
				}
				else{
					//alert when error found
					alert(json.text);
					$("a.redraw").trigger("click");
					return false;
				}
			}
		});
		
		//delete-comment button
        $(wrapper + "[id^=delete-comment-button]").click(function(e){
        	deleteId = this.id.split('-')[3]; 
        	var del=confirm("Do you want to delete this comment?");
           	if (del){	
             	$.getJSON("/en/comment/ajax/delete-comment", {'id': deleteId},
                function(json) {
	                if(json.success){	
	                    $("#comment-box-" + deleteId).fadeOut(3000); //3 seconds
	                    //hide box has pid = this id
	                    $.each(json.child, function(i, val){
                			$("#comment-box-" + val).fadeOut(3000);
                		});
	 					alert('delete successful');
	 			    }                  
	                else
	                    $("#comment-box").show().html("Could not delete");
                });
             }
             else
             	return false;
        });//end delete comment
        
        // set spam comment
        $(wrapper + "[id^=spam-comment-button]").click(function(e){
        	var spam=confirm("Do you want to spam this comment?");
        	commentId = this.id.split('-')[3];
           	if (spam){	
             	$.getJSON("/en/comment/ajax/edit-comment", {'status' : 'spam','id' : commentId},
                function(json) {
                   if (json.success)
    					alert('change status successful');
                   else
                       $("#comment-box").show().html("Could not change status");
                });
            }
            else
            	return false;
        });// end spam comment
        
         //rating comment
        $(wrapper + "[id^=rating-comment-button]").click(function(e){
        	var check = this.id.split('-')[4];
     	  	if(check == '1'){	
            	ratingId = this.id.split('-')[3];
            	var setRating = $("#rating-" + ratingId).val();
            	
            	// update rating for comment
            	$.getJSON("/en/comment/ajax/update-rating",{'id': ratingId, 'rate': setRating},
	                 function(json){  
	                     if (json.success){	
	                         $("#rating-comment-button-" + ratingId + "-" + check).hide();
	                         $("#rating-" + ratingId).hide();
	                         alert("thank you for rating this comment!");
	                     }
	                     else
	                        alert('You were rating this comment');
	            });
            }
            else{
            	alert('you must register to rating');
            	return false;
            }
       });//end rating comment
       
        //click plus
		$(wrapper + "[id^=plus]").click(function(e){
        	//get current Id & Pid & language
        	tmp = this.id.split('-');
        	plusId = tmp[1];
        	status = $(this).val();
			numberOfClicks = $(this).attr('name');
			if (numberOfClicks == '0')
				$(this).attr('name', '1');
				
        	if (status == '+' && numberOfClicks == '0'){			
				$.getJSON("/en/comment/ajax/search-children", {'id': plusId },
				function(json){
		            if (json.success){	
		            	//remove current duplicates
		            	$.each(json.ids, function(i,id){
				            $("#comment-box-" + id).remove();
				            console.log("Removing comment-box-" + id);	
          				});
		      			$(json.commentList).insertAfter("#comment-box-" + plusId).css('float', 'right');
		      			$("#children-list-" + plusId).show();
						update_dom("#children-list-" + plusId + " ");
		            }
			    });
			    $(this).val('-');
			}
		    else if (status == '+' && numberOfClicks != '0'){
		        $(this).val('-');
				$("#children-list-" + plusId).show();
			}
			else{
		        $(this).val('+');
	  		    $("#children-list-" + plusId).hide();
			}
		});//end click plus
		
		//click outter paginator
		$("a[class^=outter-pagination]").click(function(e){
			//find out page number
			className = $(this).attr('class');
			page_number = className.split('-')[2];
			var Language 			= $("#language").val();
			var Comments 			= $("#comments_per_page").val() ;
			var Set 				= $("#setOperator").val();
			var ParentContentType 	= $("#new-comment-form [name=parent_content_type]").val();
			var ParentContentId 	= $("#new-comment-form [name=parent_content_id]").val();
			
			$("#new-comment-box").appendTo("#new-comment-box-wrapper");		
			$.getJSON("/en/comment/ajax/search-by-module", 
			{'page_number' : page_number ,'language' : Language ,'comments_per_page' : Comments ,'setOperator' : Set,'parent_content_id': ParentContentId ,'parent_content_type':ParentContentType},
            function(json) {
	            if (json.success)	
	                	$("#comment-list").html(json.commentList);
	            else
	                $("#comment-box").show().html("Wrong");
	            update_dom('');
	        });
	    });
	    
	    //click inner paginator
	    $("a[class^=inner-pagination]").click(function(e){
			className 	= $(this).attr('class');
			page_number = className.split('-')[3];
			inner_Id 	= className.split('-')[2];
			$("#new-comment-box").appendTo("#new-comment-box-wrapper");	
			$.getJSON("/en/comment/ajax/search-children", 
				{'page_number' : page_number,'id' : inner_Id },
                function(json){
                  if(json.success){	
                  	//remove current duplicates
		            $.each(json.ids, function(i,id){
		            	$("#comment-box-" + id).remove();
		            	console.log("Removing comment-box-" + id);	
		           	});
                   	$("#children-list-" + inner_Id).html(json.commentList);
                   	update_dom("#children-list-" + inner_Id + " ");
  			   	  }                  
                  else
                      $("#comment-box").show().html("Wrong");
            });
		});
	}//end update-dom();
	
	//search by language comment
	$("#search-comment-box").ajaxForm({
		dataType	 : 'json',
		beforeSubmit : function move(){
			$("#new-comment-box").appendTo("#new-comment-box-wrapper");
		}, 
		success		 : function(json){
			if(json.success){
				$("#comment-list").html(json.commentList);
				update_dom('');
				return false;
			}
			else
				return false;
		}
	});//end search comment box
});//end function();
